﻿#include "WRWorkGuiConfig.h"
#include "WRWorkGuiConfigProvider.h"
#include "WRWorkMainCommandActivator.h"
#include "WRWorkMainGuiPartActivator.h"
#include "../../WRSoft-Utility/WRConfig.h"
#include "../../WRSoft-Utility/Runtime/WRAppContext.h"
#include "../../WRSoft-GuiApp-Common/WRConfig.h"
#include <QList>

namespace WRWorkGuiApp
{
    //读取功能项目。
    void readActionEntry(QList<WRWorkMainActionEntry>& list, const QDomElement& config)
    {
        QChar kindLiteral[] = {'k', 'i', 'n', 'd'};
        QString kindString = QString::fromRawData(kindLiteral, 4);
        QChar commandLiteral[] = {'C', 'o', 'm', 'm', 'a', 'n', 'd'};
        QString commandString = QString::fromRawData(commandLiteral, 7);
        QChar guiPartLiteral[] = {'G', 'u', 'i', 'P', 'a', 'r', 't'};
        QString guiPartString = QString::fromRawData(guiPartLiteral, 7);

        WRClass* _class = WRClass::select(config);
        if (_class)
        {
            QString kind = config.attribute(kindString);
            if (commandString.compare(kind,Qt::CaseInsensitive) == 0)
            {
                WRWorkMainCommandActivator* activator = _class->cast<WRWorkMainCommandActivator>(WRWorkMainCommandActivatorType);
                if (activator)
                {
                    WRWorkMainActionEntry entry(
                                WRWorkMainActionEntry::Command,
                                WRConfig::readIcon(config),
                                WRConfig::readName(config),
                                WRConfig::readTitle(config),
                                WRConfig::readDescription(config),
                                activator, config);
                    list.append(entry);
                }
            }
            else if (guiPartString.compare(kind, Qt::CaseInsensitive) == 0)
            {
                WRWorkMainGuiPartActivator* activator = _class->cast<WRWorkMainGuiPartActivator>(WRWorkMainGuiPartActivatorType);
                if (activator)
                {
                    WRWorkMainActionEntry entry(
                                WRWorkMainActionEntry::GuiPart,
                                WRConfig::readIcon(config),
                                WRConfig::readName(config),
                                WRConfig::readTitle(config),
                                WRConfig::readDescription(config),
                                activator, config);
                    list.append(entry);
                }
            }
        }
    }
    //读取功能分组。
    void readActionGroup(QList<WRWorkMainActionGroup>& list, const QDomElement& config)
    {
        QChar entryLiteral[] = {'e', 'n', 't', 'r', 'y'};
        QString entryString = QString::fromRawData(entryLiteral, 5);

        QList<WRWorkMainActionEntry> entries;
        {
            QDomElement element = config.firstChildElement(entryString);
            while(!element.isNull())
            {
                readActionEntry(entries, element);
                element = element.nextSiblingElement(entryString);
            }
        }
        if (entries.count())
        {
            WRWorkMainActionGroup group(
                        WRConfig::readName(config),
                        WRConfig::readTitle(config),
                        WRConfig::readDescription(config),
                        WRArray<WRWorkMainActionEntry>(entries));
            list.append(group);
        }
    }
    //读取功能分组。
    void readActionClass(QList<WRWorkMainActionClass>& list, const QDomElement& config)
    {
        QChar groupLiteral[] = {'g', 'r', 'o', 'u', 'p'};
        QString groupString = QString::fromRawData(groupLiteral, 5);        

        QList<WRWorkMainActionGroup> groups;
        {
            QDomElement element = config.firstChildElement(groupString);
            while(!element.isNull())
            {
                readActionGroup(groups, element);
                element = element.nextSiblingElement(groupString);
            }
        }
        if (groups.count())
        {
            WRWorkMainActionClass _class(
                        WRConfig::readIcon(config),
                        WRConfig::readName(config),
                        WRConfig::readTitle(config),
                        WRConfig::readDescription(config),
                        WRArray<WRWorkMainActionGroup>(groups));
            list.append(_class);
        }
    }
}

//初始化工作界面配置。
void WRWorkGuiConfig::init(const QDomElement& config)
{
    //图标
    m_icon = WRConfig::readIcon(config);
    //名称
    m_name = WRConfig::readName(config);
    //标题
    m_title = WRConfig::readTitle(config);
    //互斥
    QChar mutexLiteral[] = {'m', 'u', 't', 'e', 'x'};
    QString mutexString = QString::fromRawData(mutexLiteral, 5);
    m_mutex = WRConfig::readValue(config, mutexString);
    //描述
    m_description = WRConfig::readDescription(config);
    //活动
    QChar actionsLiteral[] = {'a', 'c', 't', 'i', 'o', 'n', 's'};
    QString actionsString = QString::fromRawData(actionsLiteral, 7);
    QChar classLiteral[] = {'c','l', 'a', 's', 's'};
    QString classString = QString::fromRawData(classLiteral, 5);
    QList<WRWorkMainActionClass> actions;
    {
        QDomElement element = config.firstChildElement(actionsString);
        if (!element.isNull())
        {
            element = element.firstChildElement(classString);
            while(!element.isNull())
            {
                WRWorkGuiApp::readActionClass(actions, element);
                element = element.nextSiblingElement(classString);
            }
        }
    }
    m_actions = actions;
}
//初始化界面配置，通过应用程序上下文或者面配置，如果配置错误，则返回False。
bool WRWorkGuiConfig::init(void)
{
    WRWorkGuiConfigProvider* configProvider = (WRWorkGuiConfigProvider*)WRAppContext::current()->getService(WRWorkGuiConfigProviderType);
    if (configProvider)
    {
        QDomElement configElement = configProvider->content();
        if (!configElement.isNull())
        {
            init(configElement);
            return true;
        }
    }
    return false;
}
